//
//  VisitorLoginView.swift
//  HDCWeibo
//
//  Created by 黄德超 on 16/4/7.
//  Copyright © 2016年 黄德超. All rights reserved.
//

import UIKit

@objc protocol VisitorLoginViewDelegate:NSObjectProtocol {
    //协议方法
    //登录
    func visitorWillLogin()
    //注册
    optional func visitorWillRegister()
}

class VisitorLoginView: UIView {
    
    //声明代理属性
    //属性默认是强引用 需要添加weak
    weak var visitorDelegate: VisitorLoginViewDelegate?
    
    @objc func loginDidBtnClick() {
        //代理调用协议方法
        visitorDelegate?.visitorWillLogin()
    }
    @objc func registerBtnClick() {
        //代理调用协议方法
        visitorDelegate?.visitorWillRegister?()
    }

    //MARK: 设置页面信息
    func setUIInfo(imageName: String?, title: String){
        tipLable.text = title
        iconView.hidden = false
        if imageName != nil {
            circleView.image = UIImage(named: imageName!)
            bringSubviewToFront(circleView)
            iconView.hidden = true
        }else{
            //让首页动画转起来
            startAnimation()
        }
        
    }
    
    
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath:"transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 20
        //当动画结束 或者视图处于不活跃状态时 动画不移除图层
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    //重写构造方法
    init(){
        super.init(frame: CGRectZero)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:  设置访客视图
    private func setupUI(){
       // backgroundColor = UIColor.redColor()
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //设置frame布局失效
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //circleView.translatesAutoresizingMaskIntoConstraints = false
        //iconView.translatesAutoresizingMaskIntoConstraints = false
        //设置布局
        //VFL
        //"view1.attr1 = view2.attr2 * multiplier + constant"
        //圆圈约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //大头标的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //设置提示文案约束
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 15))
        
        //设置提示文案宽度约束
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        //设置提示文案高度约束
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置登录按钮的约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLable, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLable, attribute: .Bottom, multiplier: 1.0, constant: 15))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置注册按钮的约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLable, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLable, attribute: .Bottom, multiplier: 1.0, constant: 15))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置背景图片约束
        addConstraints( NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        
        addConstraints( NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-45)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView, "registerBtn":registerBtn]))
        
        //设置背景
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        
        //添加点击事件
        loginBtn.addTarget(self, action: #selector(VisitorLoginView.loginDidBtnClick), forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: #selector(VisitorLoginView.registerBtnClick), forControlEvents: .TouchUpInside)
        //loginBtn.addTarget(self, action: #selector(VisitorLoginView.loginDidBtnClick),forControlEvents: .TouchUpInside)
        //loginBtn.addTarget(self, action: #selector(VisitorLoginView.registerBtnClick), forControlEvents: .TouchUpInside)
    }
    
    //MARK: 懒加载所有的控件
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    //提示文案
    private lazy var tipLable: UILabel = {
        let l = UILabel()
        l.text = "关注一些人,看看他们都在干什么,会有惊喜哦"
        l.textAlignment = NSTextAlignment.Center
        l.font = UIFont.systemFontOfSize(14)
        l.textColor = UIColor.lightGrayColor()
        l.numberOfLines = 0
        l.sizeToFit()
        return l
    
    }()
    
    //按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登陆", forState: .Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        return btn
        }()
    
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        return btn
    }()
}
